package com.datastore.server;

import java.util.List;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Query;

public class RuntoTestDataStore {
  public static void main(String[] arg){
    DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
    Key guestbookKey = KeyFactory.createKey("Guestbook", "default");
    
    Query query = new Query("Greeting",guestbookKey).addSort("date", Query.SortDirection.DESCENDING);
    List<Entity> greetings = datastore.prepare(query).asList(FetchOptions.Builder.withLimit(5));
    if(greetings.isEmpty()){
      
    }else{
      for(Entity greeting : greetings){
        if(greeting.getProperty("user") == null){
           System.out.println("Anynomous wrote: ");
        }else{
          System.out.println(greeting.getProperty("user")+" wrote:");
        }
        System.out.println(greeting.getProperty("content"));
      }

    }
    
  }
}
